package com.xuzimian.demo.aws.cloud;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import org.apache.commons.codec.binary.Hex;
import org.apache.tika.Tika;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.File;
import java.io.IOException;
import java.security.Key;
import java.security.SecureRandom;

public class FileTypeCheckDemo {

    public static String getMimeType(File file) {
        Tika tika = new Tika();
        try {
            return tika.detect(file);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        String key = "/local/tenantId/1479e938-56f9-4c66-9ce7-d7cb87b6e0e9.jpg";
        int lastPathSplitterIndex = key.lastIndexOf("/") + 1;
        String path = key.substring(0, lastPathSplitterIndex);
        String fileName = key.substring(lastPathSplitterIndex);
        System.out.println(path + " ------ " + fileName);

        testHutoolAESEncode();
    }

    public static String encrypt(String strClearText, String strKey) {
        String strData = "";

        try {
            SecretKeySpec skeyspec = new SecretKeySpec(strKey.getBytes(), "Blowfish");
            Cipher cipher = Cipher.getInstance("Blowfish");
            cipher.init(Cipher.ENCRYPT_MODE, skeyspec);
            byte[] encrypted = cipher.doFinal(strClearText.getBytes());
            strData = new String(encrypted);

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return strData;
    }

    public static String decrypt(String strEncrypted, String strKey) {
        String strData = "";

        try {
            SecretKeySpec skeyspec = new SecretKeySpec(strKey.getBytes(), "Blowfish");
            Cipher cipher = Cipher.getInstance("Blowfish");
            cipher.init(Cipher.DECRYPT_MODE, skeyspec);
            byte[] decrypted = cipher.doFinal(strEncrypted.getBytes());
            strData = new String(decrypted);

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return strData;
    }

    private static void testPESEncodeAndDecode(String path) {
        try {
            // 初始化盐
            SecureRandom random = new SecureRandom();
            byte[] salt = random.generateSeed(8);

            // 口令与密钥
            PBEKeySpec pbeKeySpec = new PBEKeySpec("asdfghjkl".toCharArray());
            SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWITHMD5andDES");
            Key secretKey = factory.generateSecret(pbeKeySpec);


            // 加密
            PBEParameterSpec pbeParameterSpac = new PBEParameterSpec(salt, 100);
            Cipher cipher = Cipher.getInstance("PBEWITHMD5andDES");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, pbeParameterSpac);
            byte[] result = cipher.doFinal(path.getBytes());
            System.out.println("jdk pbe encrypt:" + Hex.encodeHexString(result));


            // 解密
            cipher.init(Cipher.DECRYPT_MODE, secretKey, pbeParameterSpac);
            result = cipher.doFinal(Hex.decodeHex(Hex.encodeHexString(result)));
            System.out.println("jdk pbe decrypt:" + new String(result));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void testHutoolAESEncode() {
        String content = "test";
        AES aes = new AES(Mode.CTS, Padding.ZeroPadding, "0CoJUm6Qyw8W8jud".getBytes(), "0102030405060708".getBytes());


//加密为16进制表示
        String encryptHex = aes.encryptHex(content);
        System.out.println(encryptHex);
//解密为字符串
        String decryptStr = aes.decryptStr(encryptHex, CharsetUtil.CHARSET_UTF_8);
        System.out.println(decryptStr);
    }

}
